package com.bootdo.screen.service.impl;

import com.bootdo.screen.domain.*;
import com.bootdo.screen.service.PsareacountService;
import com.bootdo.screen.service.PsareacounttrueService;
import com.bootdo.screen.service.PssourcenumService;
import com.bootdo.screen.service.PssourcenumtrueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PsDataServiceImpl {
    @Autowired
    private PsareacountService psareacountService;

    @Autowired
    private PsareacounttrueService psareacounttrueService;

    @Autowired
    private TruefalseServiceImpl truefalseService;

    @Autowired
    private PssourcenumtrueService pssourcenumtrueService;

    @Autowired
    private PssourcenumService pssourcenumService;

    @Autowired
    private PssitenumtureServiceImpl pssitenumtureService;

    @Autowired
    private PssitenumServiceImpl pssitenumService;

    @Autowired
    private PssourcelistServiceImpl pssourcelistService;

    @Autowired
    private PshotnewServiceImpl pshotnewService;

    @Autowired
    private  PshotnewstrueServiceImpl pshotnewstrueService;

    @Autowired
    private PswarningServiceImpl pswarningService;

    @Autowired
    private PsitemmonitorServiceImpl psitemmonitorService;

    @Autowired
    private PsuserdataServiceImpl psuserdataService;

    public   Map<String, Object> getUserData(){
        Map<String, Object>  map1 =  new HashMap<String, Object>();
        map1.put("name","中国石油集团总公司");
        List<Map<String,Object>> list1 = new ArrayList<Map<String,Object>>();
        List<PsuserdataDO> pss = psuserdataService.list(null);

        for (PsuserdataDO ps: pss ) {
            int f = 0;
            Iterator<Map<String, Object>> l1it = list1.iterator();
            while (l1it.hasNext()) {
                Map<String, Object> it = l1it.next();
                if (it.get("name").equals(ps.getLevel2())) {
                    f = 1;
                    Map<String, Object>  map3 =  new HashMap<String, Object>();
                    map3.put("name",ps.getLevel3());
                    map3.put("value",ps.getNum());
                    List<Map<String,Object>> l3 = (List<Map<String,Object>>)it.get("children");
                    l3.add(map3);
                    it.put("children",l3);
                    break;
                }
            }
            if (f == 0) {
                Map<String, Object>  map2 =  new HashMap<String, Object>();
                List<Map<String,Object>> list3 = new ArrayList<Map<String,Object>>();
                Map<String, Object>  map3 =  new HashMap<String, Object>();
                map3.put("name",ps.getLevel3());
                map3.put("value",ps.getNum());
                list3.add(map3);
                map2.put("name",ps.getLevel2());
                map2.put("children",list3);
                list1.add(map2);
            }


        }
        map1.put("children",list1);
        return  map1;

    }





    public List<Map<String,Object>>  getPsItemMonitor() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<PsitemmonitorDO> pss = psitemmonitorService.list(null);
        for (PsitemmonitorDO ps:pss) {
            Map<String,Object> map = new HashMap<>();
            map.put("itemtype",ps.getItemtype());
            map.put("num",ps.getNum());
            map.put("positive",ps.getPositive());
            map.put("negative",ps.getNegative());
            map.put("Neutral",ps.getNeutral());
            list.add(map);
        }
        return  list;
    }


    public List<Map<String,Object>>  getPsWarning() {
        List<Map<String, Object>> list = new ArrayList<>();

        Map<String,Object> map2 = new HashMap<>();
        map2.put("limit",5);
        map2.put("offset",0);
        map2.put("order","desc");
        map2.put("sort","id");
        List<PswarningDO> pss = pswarningService.list(map2);
        for (PswarningDO ps:pss) {
            Map<String,Object> map = new HashMap<>();
            map.put("title",ps.getTitle());
            map.put("status",ps.getStatus());
            try {
                map.put("time", ps.getDate().getTime());
            } catch (Exception e) {
                map.put("time", "");
            }

            list.add(map);
        }
        return list;
    }

    public Map<String,Object> getPsNewsHotScore(){

        Map<String,Object> map = new HashMap<>();
        Map<String,Object> map2 = new HashMap<>();
        map2.put("limit",5);
        map2.put("offset",0);
        map2.put("order","desc");
        map2.put("sort","id");
        if(truefalse("舆情热点媒体源")==0) {
            List<PshotnewDO> pss = pshotnewService.list(map2);
            for (PshotnewDO p:pss ) {
              map.put(p.getTitle(),p.getScore());
            }
        } else {
            List<PshotnewstrueDO> pss = pshotnewstrueService.list(map2);
            for (PshotnewstrueDO p:pss ) {
                map.put(p.getTitle(),p.getScore());
            }
        }
        return map;
    }

    public Map<String,Object> getPsNewsHot(){
        List<PssourcelistDO> psslist = pssourcelistService.list(null);
        Map<String,Object> map = new HashMap<>();
        List<Map<String, Object>> data = new ArrayList<>();
        List<Map<String, Object>> links = new ArrayList<>();
        Set<String> sites = new HashSet<>();
        Map<String,Object> map2 = new HashMap<>();
        map2.put("limit",5);
        map2.put("offset",0);
        map2.put("order","desc");
        map2.put("sort","id");
        if(truefalse("舆情热点媒体源")==0) {
            List<PshotnewDO> pss = pshotnewService.list(map2);

            for (PshotnewDO p:pss ) {
                Map<String,Object> news = new HashMap<>();
                news.put("name",p.getTitle());
                news.put("category","热点新闻");
                news.put("value",p.getValue());
                data.add(news);
                String sourceList = p.getSourcelist();
                String[] sl = sourceList.split(",");
                for (String s: sl) {
                    Map<String,Object> link = new HashMap<>();
                    link.put("source",p.getTitle());
                    link.put("target",s);
                    links.add(link);
                    sites.add(s);
                }

            }

        } else {

            List<PshotnewstrueDO> pss = pshotnewstrueService.list(map2);

            for (PshotnewstrueDO ps:pss ) {
                Map<String,Object> news = new HashMap<>();
                news.put("name",ps.getTitle());
                news.put("category","热点新闻");
                news.put("value",ps.getValue());
                data.add(news);
                String sourceList = ps.getSourcelist();
                String[] sl = sourceList.split(",");
                for (String s: sl) {
                    Map<String,Object> link = new HashMap<>();
                    link.put("source",ps.getTitle());
                    link.put("target",s);
                    links.add(link);
                    sites.add(s);
                }

            }

        }
        for ( String s: sites) {

            Map<String,Object> site = new HashMap<>();
            site.put("name",s);
            site.put("category","新闻来源");
            int f =0;
            for (PssourcelistDO souce: psslist) {
                if (souce.getSource().equals(s)) {
                    site.put("value",souce.getScore());
                    f = 1;
                    break;
                }
            }
            if (f==0) {
                site.put("value", 30);
            }
            data.add(site);
        }
        map.put("data",data);
        map.put("links",links);
        return map;
    }




    public List<String>  getPsSiteList(){
        List<String> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("limit",20);
        map.put("offset",0);
        map.put("order","desc");
        map.put("sort","score");
        List<PssourcelistDO> pss = pssourcelistService.list(map);
        for (PssourcelistDO  ps:pss) {
            list.add(ps.getSource());
        }
        return list;
    }

    public List<Map<String,Object>>  getPsSiteTop(){
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("limit",10);
        map.put("offset",0);
        map.put("order","desc");
        map.put("sort","num");
        if(truefalse("舆情采集源top")==0) {
            List<PssitenumDO> pss = pssitenumService.list(map);
            for (PssitenumDO ps: pss) {
                Map<String,Object> map2 = new HashMap<>();
                map2.put("name",ps.getSite());
                map2.put("num",ps.getNum());
                list.add(map2);
            }
         } else {
            List<PssitenumtureDO> pss = pssitenumtureService.list(map);
            for (PssitenumtureDO ps: pss) {
                Map<String,Object> map2 = new HashMap<>();
                map2.put("name",ps.getSite());
                map2.put("num",ps.getNum());
                list.add(map2);
            }
        }

        return  list;
    }


    public List<Map<String,Object>>  getPsAreaCount(){

        List<Map<String, Object>> list = new ArrayList<>();
        if(truefalse("舆情地区统计")==0) {
            List<PsareacountDO> pss = psareacountService.list(null);
            for (PsareacountDO ps : pss) {
                Map<String, Object> map = new HashMap<>();
                map.put("name", ps.getArea());
                map.put("num", ps.getNum());
                list.add(map);
            }
        } else {
            List<PsareacounttrueDO> pss = psareacounttrueService.list(null);
            for (PsareacounttrueDO ps : pss) {
                Map<String, Object> map = new HashMap<>();
                map.put("name", ps.getArea());
                map.put("num", ps.getNum());
                list.add(map);
            }
        }
        return list;
    }

    public List<Map<String,Object>>  getPsSourceCount(){
        List<Map<String, Object>> list = new ArrayList<>();
        if(truefalse("舆情数据来源统计")==0) {
            List<PssourcenumDO> pss = pssourcenumService.list(null);
            for (PssourcenumDO ps : pss) {
                Map<String, Object> map = new HashMap<>();
                map.put("name", ps.getSource());
                map.put("num", ps.getNum());
                list.add(map);
            }
        } else {
            List<PssourcenumtrueDO> pss = pssourcenumtrueService.list(null);
            for (PssourcenumtrueDO ps : pss) {
                Map<String, Object> map = new HashMap<>();
                map.put("name", ps.getSource());
                map.put("num", ps.getNum());
                list.add(map);
            }
        }
        return list;
    }


    private int truefalse(String name){
        Map<String, Object>  map =  new HashMap<String, Object>();
        map.put("name",name);
        List<TruefalseDO> re = truefalseService.list(map);
        if (re.isEmpty()) {
            return 1;
        }
        return re.get(0).getValue();
    }
}
